﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SQLResident.Models;

namespace SQLResident.Controllers
{ 
    public class AlertLogController : Controller
    {
        private SQLResidentEntities db = new SQLResidentEntities();

        //
        // GET: /AlertLog/

        public ViewResult Index()
        {
            var alertlog = db.AlertLog.Include("AlertConfiguration").Include("BlockingProcessLog");
            return View(alertlog.ToList());
        }

        //
        // GET: /AlertLog/Details/5

        public ViewResult Details(int id)
        {
            AlertLog alertlog = db.AlertLog.Single(a => a.AlertLogId == id);
            return View(alertlog);
        }

        //
        // GET: /AlertLog/Create

        public ActionResult Create()
        {
            ViewBag.AlertConfigurationId = new SelectList(db.AlertConfiguration, "AlertConfigurationId", "AlertConfigurationId");
            ViewBag.BlockingProcessId = new SelectList(db.BlockingProcessLog, "BlockingProcessId", "LastWaitType");
            return View();
        } 

        //
        // POST: /AlertLog/Create

        [HttpPost]
        public ActionResult Create(AlertLog alertlog)
        {
            if (ModelState.IsValid)
            {
                db.AlertLog.AddObject(alertlog);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.AlertConfigurationId = new SelectList(db.AlertConfiguration, "AlertConfigurationId", "AlertConfigurationId", alertlog.AlertConfigurationId);
            ViewBag.BlockingProcessId = new SelectList(db.BlockingProcessLog, "BlockingProcessId", "LastWaitType", alertlog.BlockingProcessId);
            return View(alertlog);
        }
        
        //
        // GET: /AlertLog/Edit/5
 
        public ActionResult Edit(int id)
        {
            AlertLog alertlog = db.AlertLog.Single(a => a.AlertLogId == id);
            ViewBag.AlertConfigurationId = new SelectList(db.AlertConfiguration, "AlertConfigurationId", "AlertConfigurationId", alertlog.AlertConfigurationId);
            ViewBag.BlockingProcessId = new SelectList(db.BlockingProcessLog, "BlockingProcessId", "LastWaitType", alertlog.BlockingProcessId);
            return View(alertlog);
        }

        //
        // POST: /AlertLog/Edit/5

        [HttpPost]
        public ActionResult Edit(AlertLog alertlog)
        {
            if (ModelState.IsValid)
            {
                db.AlertLog.Attach(alertlog);
                db.ObjectStateManager.ChangeObjectState(alertlog, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AlertConfigurationId = new SelectList(db.AlertConfiguration, "AlertConfigurationId", "AlertConfigurationId", alertlog.AlertConfigurationId);
            ViewBag.BlockingProcessId = new SelectList(db.BlockingProcessLog, "BlockingProcessId", "LastWaitType", alertlog.BlockingProcessId);
            return View(alertlog);
        }

        //
        // GET: /AlertLog/Delete/5
 
        public ActionResult Delete(int id)
        {
            AlertLog alertlog = db.AlertLog.Single(a => a.AlertLogId == id);
            return View(alertlog);
        }

        //
        // POST: /AlertLog/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            AlertLog alertlog = db.AlertLog.Single(a => a.AlertLogId == id);
            db.AlertLog.DeleteObject(alertlog);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}